package com.ljkj.blockpay.basedata;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.WindowManager;

import com.ljkj.blockpay.util.BarUtils;
import com.trello.rxlifecycle.components.support.RxAppCompatActivity;

import org.greenrobot.eventbus.EventBus;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

/**
 * Created by tianping on 2020/1/2.
 */

public abstract class BaseActivity2<V extends ViewDataBinding, P extends BasePresenterImpl> extends RxAppCompatActivity {
    protected V binding;
    private P mPresenter;//功能调用
    //    private ImmersionBar mImmersionBar;
    protected Activity baseContext;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        this.baseContext = BaseActivity2.this;
        ActivityCollector.addActivity(this);
        setMarquee();
        initViewDataBinding();
        setPresenter();
        init(savedInstanceState);
        initData();
        BarUtils.setStatusBarAlpha(this, 0);
        BarUtils.setStatusBarLightMode(this, true);

        compatible();
    }

    /**
     * 注入绑定
     */
    private void initViewDataBinding() {
        //DataBindingUtil类需要在project的build中配置 dataBinding {enabled true }, 同步后会自动关联android.databinding包
        binding = DataBindingUtil.setContentView(this, initContentView());
    }

    /**
     * 初始化根布局
     *
     * @return 布局layout的id
     */
    public abstract int initContentView();

    /**
     * 初始化网络请求presenter
     *
     * @return 布局layout的id
     */
    protected abstract P createPresenter();

    protected abstract void init(Bundle savedInstanceState);

    /**
     * 获取presenter
     *
     * @return
     */
    protected P getPresenter() {
        return mPresenter;
    }

    protected void setPresenter() {
        if (mPresenter == null)
            mPresenter = createPresenter();
    }

    protected void initData() {

    }

    public boolean isFinish(){
        return isFinishing() || isDestroyed();
    }
    protected void postUIThread(final Runnable run) {
        if (isFinish())
            return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (isFinish())
                    return;
                run.run();
            }
        });
    }

    // 判断权限集合
    protected boolean needPermissions(String... permissions) {
        //判断版本是否兼容
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return false;
        }
        boolean isNeed;
        for (String permission : permissions) {
            isNeed = needsPermission(permission);
            if (isNeed) {
                return true;
            }
        }
        return false;
    }

    protected List<String> noPermissions(String... permissions) {
        List<String> list = new ArrayList<>();
        for (String permission : permissions) {
            if (needPermissions(permission)) {
                list.add(permission);
            }
        }
        return list;
    }

    // 判断是否缺少权限
    protected boolean needsPermission(String permission) {
        return ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED;
    }

    /**
     * 注册全局监听事件
     */
    protected void registerEventListener() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    /**
     * 注销全局监听事件
     */
    protected void removeEventListener() {
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
            EventBus.getDefault().removeAllStickyEvents();
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        removeEventListener();
        ActivityCollector.removeActivity(this);
//        if (mImmersionBar != null) {
//            mImmersionBar.destroy();
//        }
        super.onDestroy();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void compatible() {
        //兼容8.1系统输入框获取焦点问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            getWindow().getDecorView().setImportantForAutofill(
                    View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
        }
    }

    private void setMarquee() {
        Class viewConfiguration = ViewConfiguration.get(this).getClass();
        for (Field field : viewConfiguration.getDeclaredFields()) {
            if ("mFadingMarqueeEnabled".equalsIgnoreCase(field.getName())) {
                field.setAccessible(true);
                try {
                    field.setBoolean(ViewConfiguration.get(this), true);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
